package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JProgressBar;
import javax.swing.JSeparator;
import javax.swing.JMenuBar;
import javax.swing.ListSelectionModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.ResultSet;

import javax.swing.JScrollPane;

import controllers.DBConnection;
import javax.swing.ImageIcon;

public class ShowPurchasesPictures extends JFrame {

	private JPanel contentPane;
	static String ID;
	int pictureid;
	byte[] fileBytes;
	public String url13;
	public String url14;
	public String url15;
	public String url16;
	public String s;
	public String picIdentety;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ShowPurchasesPictures frame = new ShowPurchasesPictures(ID);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the Show Purchases Pictures frame.
	 * @throws IOException 
	 */
	public ShowPurchasesPictures(String Id) throws IOException {
		this.ID=Id;
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 629, 365);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		this.setTitle("Your Purchases");
		
		
		
		
		final JList<String> list = new JList<String>();
		list.setBounds(38, 51, 83, 166);
		list.setVisibleRowCount(10);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setBorder(null);
		list.addComponentListener(new ComponentAdapter() {
			
			public void componentMoved(ComponentEvent arg0) {
			}
		});
		
		try
			{
			
				DefaultListModel<String> model = new DefaultListModel<String>();
				DBConnection conn1=new DBConnection();
				java.sql.PreparedStatement pr= conn1.getDBConnection().prepareStatement("SELECT picID FROM sgi_system.purchasespic WHERE customerID=? ");
				pr.setString(1, ID);
				ResultSet rs = pr.executeQuery();
			
				while(rs.next()){
					this.pictureid=rs.getInt("picID");
					if (rs.getInt("picID")%2==0)
					model.addElement(String.valueOf(pictureid));
				}
				System.out.println(model);
				list.setModel(model);
				
				
				
			}//try
			catch (Exception e){e.printStackTrace();}
		
		
		
		
		
		JLabel lblListOfPurchases = new JLabel("List of Pictures : ");
		lblListOfPurchases.setBounds(38, 26, 141, 14);
		
		JButton btnReturn = new JButton("Back");
		btnReturn.setBounds(21, 228, 69, 23);
		btnReturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CustomerGui frame = new CustomerGui(ID);
				frame.setVisible(true);
				dispose();
			}
		});
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.setBounds(96, 228, 83, 23);
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		
		JButton btnViewPicture = new JButton("View Picture");
		btnViewPicture.setBounds(149, 90, 125, 71);
		btnViewPicture.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				try{
				DBConnection conn1=new DBConnection();
				
				s=(".jpg");
				picIdentety=(list.getSelectedValue()+s);
				System.out.println(picIdentety);
				java.sql.PreparedStatement pr8= conn1.getDBConnection().prepareStatement("SELECT * FROM sgi_system.image WHERE picID=? ");
				pr8.setString(1,picIdentety);
				ResultSet rs = pr8.executeQuery();
				while(rs.next())
				{
	                fileBytes = rs.getBytes(3);
	                url13=("C://picc//image//");
	                url14=picIdentety;
	                OutputStream targetFile=new FileOutputStream(url13+url14);
	                targetFile.write(fileBytes);
	                targetFile.close();
				}//while
				}catch(Exception e1){e1.printStackTrace();}
				 MapScale test = new MapScale(url13+url14);
				 try {
					test.Run();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				}
			
			
		});
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(316, 90, 2, 2);
		contentPane.setLayout(null);
		contentPane.add(lblListOfPurchases);
		contentPane.add(btnReturn);
		contentPane.add(btnCancel);
		contentPane.add(list);
		contentPane.add(scrollPane);
		contentPane.add(btnViewPicture);
		
		final JList list_1 = new JList();
		list_1.addComponentListener(new ComponentAdapter() {
			
			public void componentMoved(ComponentEvent arg0) {
			}
		});
		try
		{
		
			DefaultListModel<String> model = new DefaultListModel<String>();
			DBConnection conn1=new DBConnection();
			java.sql.PreparedStatement pr= conn1.getDBConnection().prepareStatement("SELECT picID FROM sgi_system.purchasespic WHERE customerID=? ");
			pr.setString(1, ID);
			ResultSet rs = pr.executeQuery();
		
			while(rs.next()){
				this.pictureid=rs.getInt("picID");
				if (rs.getInt("picID")%2==1)
				model.addElement(String.valueOf(pictureid));
			}
			System.out.println(model);
			list_1.setModel(model);
			
			
			
		}//try
		catch (Exception e){e.printStackTrace();}
	
	
		list_1.setBounds(429, 51, 117, 172);
		contentPane.add(list_1);
		
		JLabel lblListOfSamples = new JLabel("List of Samples : ");
		lblListOfSamples.setBounds(428, 26, 118, 14);
		contentPane.add(lblListOfSamples);
		
		JButton btnNewButton = new JButton("View Sample");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				
				try{
				DBConnection conn1=new DBConnection();
				
				s=(".jpg");
				picIdentety=(list_1.getSelectedValue()+s);
				System.out.println(picIdentety);
				java.sql.PreparedStatement pr9= conn1.getDBConnection().prepareStatement("SELECT * FROM sgi_system.sample WHERE sampleID=? ");
				pr9.setString(1,picIdentety);
				ResultSet rs9 = pr9.executeQuery();
				while(rs9.next())
				{
	                fileBytes = rs9.getBytes(3);
	                url15=("C://picc//Sample//");
	                url16=picIdentety;
	                OutputStream targetFile=new FileOutputStream(url15+url16);
	                targetFile.write(fileBytes);
	                targetFile.close();
				}//while
				}catch(Exception e1){e1.printStackTrace();}
				 MapScale test = new MapScale(url15+url16);
				 try {
					test.Run();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		btnNewButton.setBounds(284, 90, 131, 71);
		contentPane.add(btnNewButton);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon("images//sea.jpg"));
		lblNewLabel.setBounds(0, 0, 613, 326);
		contentPane.add(lblNewLabel);
		lblNewLabel.setLayout(null);
	}
}
